What is vite-svg-loader?
vite-svg-loader is a Vite plugin that allows you to import SVG files as Vue components. This can be particularly useful for projects using Vue.js, as it enables you to easily manipulate and style SVGs using Vue's reactivity system.
What are vite-svg-loader's main functionalities?
Import SVG as Vue Component
This feature allows you to import an SVG file and use it as a Vue component. This makes it easy to include and manipulate SVGs within your Vue templates.
import MyIcon from './my-icon.svg';
export default {
components: {
MyIcon
},
template: '<MyIcon />'
};
Inline SVGs
By appending '?inline' to the import statement, you can import the SVG as an inline string. This can be useful for directly embedding SVGs into your HTML.
import MyIcon from './my-icon.svg?inline';
export default {
template: '<div v-html="MyIcon" />'
};
SVG Optimization
vite-svg-loader supports SVG optimization using SVGO. You can pass SVGO configuration options to optimize your SVGs during the build process.
import MyIcon from './my-icon.svg';
// vite.config.js
import { defineConfig } from 'vite';
import svgLoader from 'vite-svg-loader';
export default defineConfig({
plugins: [svgLoader({ svgoConfig: { /* SVGO options */ } })]
});
Other packages similar to vite-svg-loader
vite-plugin-svg-icons
vite-plugin-svg-icons is another Vite plugin that allows you to use SVG icons in your project. It provides a similar functionality to vite-svg-loader but focuses more on managing a collection of SVG icons and generating an icon component library.
vite-plugin-vue-svg
vite-plugin-vue-svg is a Vite plugin that also allows you to import SVG files as Vue components. It offers similar functionality to vite-svg-loader but may have different configuration options and defaults.
vite-plugin-svgr
vite-plugin-svgr is a Vite plugin that transforms SVGs into React components. While it is designed for React rather than Vue, it provides similar functionality in terms of importing and using SVGs as components.
Vite SVG loader
Vite plugin to load SVG files as Vue components, using SVGO for optimization.
<template>
<MyIcon />
</template>
<script setup>
import MyIcon from './my-icon.svg'
</script>
Install
npm install vite-svg-loader --save-dev
Setup
vite.config.js
import svgLoader from 'vite-svg-loader'
export default defineConfig({
plugins: [vue(), svgLoader()]
})
Import params
URL
SVGs can be imported as URLs using the ?url
suffix:
import iconUrl from './my-icon.svg?url'
Raw
SVGs can be imported as strings using the ?raw
suffix:
import iconRaw from './my-icon.svg?raw'
Component
SVGs can be explicitly imported as Vue components using the ?component
suffix:
import IconComponent from './my-icon.svg?component'
Default import config
When no explicit params are provided SVGs will be imported as Vue components by default.
This can be changed using the defaultImport
config setting,
such that SVGs without params will be imported as URLs (or raw strings) instead.
vite.config.js
svgLoader({
defaultImport: 'url'
})
SVGO Configuration
vite.config.js
svgLoader({
svgoConfig: {
multipass: true
}
})
Disable SVGO
vite.config.js
svgLoader({
svgo: false
})
Skip SVGO for a single file
SVGO can be explicitly disabled for one file by adding the ?skipsvgo
suffix:
import IconWithoutOptimizer from './my-icon.svg?skipsvgo'
Use with TypeScript
If you use the loader in a Typescript project, you'll need to reference the type definitions inside vite-env.d.ts
:
Thanks to Nexxtmove for sponsoring the development of this project.
Your logo or name here? Sponsor this project.